<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Storage;

/**
 * Class HuatiParent
 * @package App
 */
class HuatiParent extends Model
{
    public $timestamps = false;
    protected $table = 'tbl_huati_parent';


    //是否推荐
    const HOT_YES = 1;
    const HOT_NO = 0;
    static $HotDisplay = [
        self::HOT_YES => '推荐',
        self::HOT_NO => '不推荐',
    ];

    /**
     * 话题列表
     * @return \Illuminate\Support\Collection
     */
    static function Huatis()
    {
        return HuatiParent::select('id', 'title')->get();
    }


    /**
     * 修改话题
     * @param $request
     * @param $huati
     * @return array
     */
    static function ModifyHuatip($request, $huati)
    {
        $huati->title = $request->title;
        $huati->weight = $request->weight;

        if ($request->has('hot')) {
            $huati->hot = self::HOT_YES;
        } else {
            $huati->hot = self::HOT_NO;
        }

        //图片
        if ($request->has('pic')) {
            $pic = Storage::url(Postings::OssUpdate($request->pic));
            $huati->pic = $pic;

        }

        if (!$huati->save()) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 添加
     * @param $request
     * @return array
     */
    static function HuatipPublish($request)
    {
        $huati = new HuatiParent();
        $huati->title = $request->title;
        $huati->weight = $request->weight;
        //是否推荐
        if ($request->has('hot')) {
            $huati->hot = self::HOT_YES;
        } else {
            $huati->hot = self::HOT_NO;
        }

        //图片
        if ($request->has('pic')) {
            $pic = Storage::url(Postings::OssUpdate($request->pic));
            $huati->pic = $pic;
        } else {
            return ['status' => 2, 'message' => '失败'];
        }

        if (!$huati->save()) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];
    }

}
